from QSizer import QSizer, QSizerGet, ObjectHeightGet, ObjectWidthGet, ObjectPosSizeSet

class GridSizer(QSizer):
    def __init__(self, col, row = None, **kw):
        QSizer.__init__(self, **kw)

        self.Col = col
        self.Row = kw.get("row", None)
        self.Vgap = kw.get("vgap", 0)
        self.Hgap = kw.get("hgap", 0)
        self._maxItemHeight, self._maxItemWidth = None, None

    def Layout(self):
        # does outer information available?
        if (not self.pos) or (not self.size):
            raise "GridSizer Can not layout Sizer"

        self.PropertySet()
        SlotList = self.SlotList

        # get item height, item width
        if not self._maxItemHeight:
            self._maxItemHeight = max([ObjectHeightGet(item["guiObject"]) for item in SlotList])
        if not self._maxItemWidth:
            self._maxItemWidth = max([ObjectWidthGet(item["guiObject"]) for item in SlotList])

        if self._maxItemHeight == 0:
            self._maxItemHeight = (self._TopZ - self._BottomZ - (self.Vgap*(self.Row-1)))/float(self.Row)
        if self._maxItemWidth == 0:
            self._maxItemWidth = (self._RightX - self._LeftX - (self.Hgap*(self.Col-1)))/float(self.Col)

        # set object size
        for index, item in enumerate(SlotList): self.PosSizeSet(index, item)

        # call Sizer.Layout
        for item in SlotList:
            sizer = QSizerGet(item["guiObject"])
            if sizer: sizer.Layout()

    def SizerHightGet(self):
        if not self._maxItemHeight:
            self._maxItemHeight = max([ObjectHeightGet(item["guiObject"]) for item in self.SlotList])
        if not self.Row: self.Row = self.RowCntGet()
        ret = self._maxItemHeight*self.Row + (self.Vgap*(self.Row-1))
        return ret

    def SizerWidthGet(self):
        if not self._maxItemWidth:
            self._maxItemWidth = max([ObjectWidthGet(item["guiObject"]) for item in self.SlotList])
        ret = self._maxItemWidth*self.Col + (self.Hgap*(self.Col-1))
        return ret

    def PropertySet(self):
        self._LeftX, self._RightX, self._BottomZ, self._TopZ = QSizer.BoundaryGet(self)
        self._TopBound, self._LeftBound = self._TopZ, self._LeftX
        if not self.Row: self.Row = self.RowCntGet()

    def RowCntGet(self):
        itemCnt = len(self.SlotList)
        row = (itemCnt/self.Col) + ((itemCnt%self.Col) != 0)
        return row

    def PosSizeSet(self, index, item):
        guiObject, flag = item["guiObject"], item["flag"]
        rightBound, bottomBound = (self._LeftBound + self._maxItemWidth), (self._TopBound - self._maxItemHeight)
        sizerBoundXZ = (self._LeftBound, rightBound, bottomBound, self._TopBound)
        flagXZ = (flag, flag)
        ObjectPosSizeSet(guiObject, sizerBoundXZ, flagXZ)

        if ((index+1)%self.Col)==0:
            self._TopBound, self._LeftBound = (bottomBound - self.Vgap), self._LeftX
            print self._TopBound, bottomBound, self.Vgap
        else:
            self._LeftBound = rightBound + self.Hgap
